var kb = {
    'a': [0, 0], 'b': [0, 1], 'c': [0, 2], 'd': [0, 3], 'e': [0, 4], '1': [0, 5], '2': [0, 6], '3': [0, 7],
    'f': [1, 0], 'g': [1, 1], 'h': [1, 2], 'i': [1, 3], 'j': [1, 4], '4': [1, 5], '5': [1, 6], '6': [1, 7],
    'k': [2, 0], 'l': [2, 1], 'm': [2, 2], 'n': [2, 3], 'o': [2, 4], '7': [2, 5], '8': [2, 6], '9': [2, 7],
    'p': [3, 0], 'q': [3, 1], 'r': [3, 2], 's': [3, 3], 't': [3, 4], '.': [3, 5], '@': [3, 6], '0': [3, 7],
    'u': [4, 0], 'v': [4, 1], 'w': [4, 2], 'x': [4, 3], 'y': [4, 4], 'z': [4, 5], '_': [4, 6], '/': [4, 7],
    'aA': [5, 0], 'SP': [5, 1]
};
var kb1 = {
    'A': [0, 0], 'B': [0, 1], 'C': [0, 2], 'D': [0, 3], 'E': [0, 4], '1': [0, 5], '2': [0, 6], '3': [0, 7],
    'F': [1, 0], 'G': [1, 1], 'H': [1, 2], 'I': [1, 3], 'J': [1, 4], '4': [1, 5], '5': [1, 6], '6': [1, 7],
    'K': [2, 0], 'L': [2, 1], 'M': [2, 2], 'N': [2, 3], 'O': [2, 4], '7': [2, 5], '8': [2, 6], '9': [2, 7],
    'P': [3, 0], 'Q': [3, 1], 'R': [3, 2], 'S': [3, 3], 'T': [3, 4], '.': [3, 5], '@': [3, 6], '0': [3, 7],
    'U': [4, 0], 'V': [4, 1], 'W': [4, 2], 'X': [4, 3], 'Y': [4, 4], 'Z': [4, 5], '_': [4, 6], '/': [4, 7],
    'aA': [5, 0], 'SP': [5, 1]
};
var kb2 = {
    '^': [0, 0], '~': [0, 1], '?': [0, 2], '!': [0, 3], '\'': [0, 4], '"': [0, 5], '(': [0, 6], ')': [0, 7],
    '-': [1, 0], ':': [1, 1], ';': [1, 2], '+': [1, 3], '&': [1, 4], '%': [1, 5], '*': [1, 6], '=': [1, 7],
    '<': [2, 0], '>': [2, 1], '€': [2, 2], '£': [2, 3], '$': [2, 4], '¥': [2, 5], '¤': [2, 6], '\\': [2, 7],
    '[': [3, 0], ']': [3, 1], '{': [3, 2], '}': [3, 3], ',': [3, 4], '.': [3, 5], '@': [3, 6], '§': [3, 7],
    '#': [4, 0], '¿': [4, 1], '¡': [4, 2], '_': [4, 6], '/': [4, 7],
    'aA': [5, 0], 'SP': [5, 1]
};
var kbs = [kb, kb1, kb2];
var tvRemote = function (words) {
    var width = 8;
    var height = 6;
    const dist = (A, B) => {
        var dx = Math.abs(A[1] - B[1]);
        var dy = Math.abs(A[0] - B[0]);
        var a = dx + dy;
        var b = width + dy - dx;
        var c = height + dx - dy;
        var d = width + height - dx - dy;
        return Math.min(...[a, b, c, d])
    }
    var mode = 0;
    var cur_pos = [0, 0];
    var d = 0


    for (c of words) {
        if (/\d/.test(c) && mode === 2) {
            d += dist(cur_pos, kbs[mode]['aA']);
            d += 1;
            mode = 0;
            cur_pos = kbs[mode]['aA'];
        } else if (/[^\w@./\s]/i.test(c) && mode != 2) {
            //switch to mode2
            d += dist(cur_pos, kbs[mode]['aA']);
            d += 2 - mode;
            mode = 2;
            cur_pos = kbs[mode]['aA'];
        } else if (/[A-Z]/.test(c) && mode != 1) {
            d += dist(cur_pos, kbs[mode]['aA']);
            d += mode ? 2 : 1;
            mode = 1;
            cur_pos = kbs[mode]['aA'];
        } else if (/[a-z]/.test(c) && mode != 0) {
            d += dist(cur_pos, kbs[mode]['aA']);
            d += mode === 1 ? 2 : 1;
            mode = 0;
            cur_pos = kbs[mode]['aA'];
        }
        next_pos = (/\s/.test(c) ? kbs[mode]['SP'] : kbs[mode][c]);
        if (!next_pos || !cur_pos) {
            console.log(c);
        }
        d += dist(next_pos, cur_pos) + 1;
        cur_pos = next_pos;
    }
    return d;
}
